package com.futureway.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.futureway.test.entity.Order;
import com.futureway.test.entity.OrderItem;
import com.futureway.test.service.OrderService;
import com.futureway.test.util.SnowflakeIdWorker;
import com.futureway.test.util.id.IdWorker;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

	@Autowired
	private OrderService orderService;

	@PostMapping("/save")
	public void save(@RequestBody Order order) {
		log.info("params is:{}", order);

		IdWorker idWorker = new SnowflakeIdWorker(0, 0);

		order.setId(String.valueOf(idWorker.createId()));
		if (order.getItems() != null)
			order.getItems().forEach(it -> {
				it.setId(String.valueOf(idWorker.createId()));
				it.setOrder(order);
			});

		// List<OrderItem> items = new ArrayList<>();
		// String orderId = String.valueOf(idWorker.createId());
		// Order _order = new Order(orderId, "订单三", items);
		// _order.addItem(new OrderItem(String.valueOf(idWorker.createId()), "红叉"));
		// _order.addItem(new OrderItem(String.valueOf(idWorker.createId()), "电刀"));
		// _order.addItem(new OrderItem(String.valueOf(idWorker.createId()), "相位鞋"));
		log.info("params after:{}", order);
		orderService.save(order);
	}

	@PostMapping("/update")
	public void update() {
		IdWorker idWorker = new SnowflakeIdWorker(0, 0);
		Order order = new Order("301371710504960", "订单四");
		order.addItem(new OrderItem("301371710504962", "冰甲改"));
		order.addItem(new OrderItem(String.valueOf(idWorker.createId()), "春哥甲"));
		orderService.save(order);
	}

	@GetMapping("/delete/item")
	public void deleteItem() {
		String itemId = "302363709538304";
		orderService.deleteItem(itemId);
	}

}
